/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solucionessgh.ContactoHuesped;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;
/**
 *
 * @author roderick
 */
public class ContactoHuespedModel extends DataBase{
    private int id;
    private String cntdesc;


    public ContactoHuespedModel(){

    }

    private boolean validar(){
        return true;

    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("insert into contactos_huesped(cntdesc) values(?);");
                stmt.setString(1,this.cntdesc);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Contacto Huesped",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("update contactos_huesped set cntdesc = ? where cntcod = ? ;");
                stmt.setString(1,this.cntdesc);
                stmt.setInt(3, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public boolean delete(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from contactos_huesped where cntcod=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT * FROM contactos_huesped order by cntdesc ");
    }


    public ResultSet finder(String var){
        return this.find("SELECT cntcod, cntdesc FROM contactos_huesped WHERE cntdesc LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){
        return this.find("SELECT cntcod, cntdesc FROM contactos_huesped WHERE cntcod = "+var+";");
    }



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public String getCntdesc() {
        return cntdesc;
    }

    public void setCntdesc(String cntdesc) {
        this.cntdesc = cntdesc;
    }
}
